package com.zhangkunhao.notes.view;

import com.zhangkunhao.notes.controller.LoginListener;

import java.awt.*;
import javax.swing.*;

/**
 * 登录页面
 *
 * @author 张坤浩
 */
public class LoginView extends JFrame {
    public LoginView() {
    }

    /**
     * 初始化登录页面
     */
    public void initComponents() {
        JPanel panel2 = new JPanel();
        JTextField userNameText = new JTextField();
        JPasswordField passwordField = new JPasswordField();
        JLabel label1 = new JLabel();
        JLabel label2 = new JLabel();
        JButton registerButton = new JButton();
        JButton loginButton = new JButton();
        JButton adminButton = new JButton();

        //======== this ========
        setBackground(Color.white);
        setTitle("功德系统");
        setForeground(SystemColor.text);
        setFont(new Font(Font.DIALOG, Font.PLAIN, 18));
        Container contentPane = getContentPane();
        contentPane.setLayout(null);
        //======== panel2 ========
        {
            panel2.setForeground(new Color(0x999999));
            panel2.setBackground(new Color(0x99ffff));
            panel2.setLayout(null);
            {
                // compute preferred size
                Dimension preferredSize = new Dimension();
                for (int i = 0; i < panel2.getComponentCount(); i++) {
                    Rectangle bounds = panel2.getComponent(i).getBounds();
                    preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                    preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                }
                Insets insets = panel2.getInsets();
                preferredSize.width += insets.right;
                preferredSize.height += insets.bottom;
                panel2.setMinimumSize(preferredSize);
                panel2.setPreferredSize(preferredSize);
            }
        }
        contentPane.add(panel2);
        panel2.setBounds(new Rectangle(new Point(610, 400), panel2.getPreferredSize()));
        contentPane.add(userNameText);
        userNameText.setBounds(205, 55, 265, 45);
        contentPane.add(passwordField);
        passwordField.setBounds(210, 140, 270, 50);
        //---- label1 ----
        label1.setText("用户名：");
        label1.setIconTextGap(10);
        label1.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 18));
        contentPane.add(label1);
        label1.setBounds(110, 50, 80, 55);
        //---- label2 ----
        label2.setText("密码：");
        label2.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 18));
        contentPane.add(label2);
        label2.setBounds(115, 135, 60, 55);
        //---- button3 ----
        registerButton.setText("用户注册");
        contentPane.add(registerButton);
        registerButton.setBounds(165, 260, 90, 40);
        //---- button4 ----
        loginButton.setText("用户登录");
        contentPane.add(loginButton);
        loginButton.setBounds(285, 260, 100, 40);
        //---- button5 ----
        adminButton.setText("管理员登录");
        contentPane.add(adminButton);
        adminButton.setBounds(410, 260, 100, 40);
        {
            // compute preferred size
            Dimension preferredSize = new Dimension();
            for (int i = 0; i < contentPane.getComponentCount(); i++) {
                Rectangle bounds = contentPane.getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = contentPane.getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            contentPane.setMinimumSize(preferredSize);
            contentPane.setPreferredSize(preferredSize);
        }
        pack();
        setLocationRelativeTo(getOwner());
        //设置可见
        this.setVisible(true);

        //绑定事件,调用controller层
        LoginListener ll = new LoginListener(this, userNameText, passwordField);
        loginButton.addActionListener(ll);
        registerButton.addActionListener(ll);
        adminButton.addActionListener(ll);
    }
}
